package com.example.javamediaserver.controller;

import com.example.javamediaserver.model.StreamSession;
import com.example.javamediaserver.rtmp.RtmpServer;
import com.example.javamediaserver.service.StreamSessionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/streams")
@RequiredArgsConstructor
@Slf4j
@CrossOrigin
public class StreamController {

    private final StreamSessionService streamSessionService;
    private final RtmpServer rtmpServer;
    
    // 获取活动流列表
    @GetMapping
    public ResponseEntity<Map<String, Object>> getActiveStreams() {
        try {
            List<StreamSession> activeStreams = streamSessionService.findActiveSessions();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("count", activeStreams.size());
            response.put("streams", activeStreams);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取活动流列表失败", e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());
            
            return ResponseEntity.status(500).body(response);
        }
    }
    
    // 获取特定流
    @GetMapping("/{sessionId}")
    public ResponseEntity<Map<String, Object>> getStream(@PathVariable String sessionId) {
        try {
            return streamSessionService.findSession(sessionId)
                    .map(session -> {
                        Map<String, Object> response = new HashMap<>();
                        response.put("success", true);
                        response.put("stream", session);
                        
                        return ResponseEntity.ok(response);
                    })
                    .orElseGet(() -> {
                        Map<String, Object> response = new HashMap<>();
                        response.put("success", false);
                        response.put("error", "Stream not found");
                        
                        return ResponseEntity.status(404).body(response);
                    });
        } catch (Exception e) {
            log.error("获取流失败: {}", sessionId, e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());
            
            return ResponseEntity.status(500).body(response);
        }
    }
    
    // 获取统计信息
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getStats() {
        try {
            List<StreamSession> activeStreams = streamSessionService.findActiveSessions();
            List<StreamSession> recordingSessions = streamSessionService.findRecordingSessions();
            
            Map<String, Object> stats = new HashMap<>();
            stats.put("activeStreams", activeStreams.size());
            stats.put("recordingSessions", recordingSessions.size());
            stats.put("rtmpSessions", rtmpServer.getActiveSessions().size());
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("stats", stats);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取统计信息失败", e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());
            
            return ResponseEntity.status(500).body(response);
        }
    }
} 